package com.miyake.dsp.osa.lab.wrapper;

import com.miyake.dsp.generator.FpLdSetting;
import com.miyake.dsp.osa.lab.Calculator;
import com.miyake.dsp.osa.lab.LabUiAnnotation;
@LabUiAnnotation(caption="FP-LD")
public class FpLdWrapper extends Calculator {
//	public static final String OUT_FREQUENCY = "Frequency";
//	public static final String OUT_LEVELF = "Level (for Frequency)";
	public static final String OUT_WAVELENGTH = "Wavelength";
	public static final String OUT_LEVELW = "Level";
	private FpLdSetting setting;
	
	public FpLdWrapper(FpLdSetting setting) {
		this.setting = setting;
//		initOutput(OUT_FREQUENCY);
//		initOutput(OUT_LEVELF);
		initOutput(OUT_WAVELENGTH);
		initOutput(OUT_LEVELW);
	}
	
	@Override
	public synchronized void process() {
		double L = setting.getCavityLength();//400e-6;
		double n = setting.getRefractiveIndex();//3.5;
		double[] px = new double[setting.getModes()];
//		double[] py = new double[px.length];
		int q = setting.getStartMode();
		for (int i = 0; i < px.length; i++) {
			px[i] = 2.0 * n * L / (double)q;
//			py[i] = Math.random();
			q++;
		}
		
		double[] x = new double[setting.getDataLength()];
		double[] y = new double[x.length];
		int current = px.length - 1;
		double start = px[px.length - 1] - (px[px.length - 2] - px[px.length - 1]) / 2.0;
		double step = (px[0] - px[px.length-1]) / (double)setting.getDataLength();
		for (int i = 0; i < x.length; i++) {
			x[i] = start + (double)i * step;
			if (current >= 0 && x[i] > px[current]) {
				y[i] = 1;
				current--;
			}
			else {
				y[i] = 0;
			}
			
		}
		setOutput(OUT_WAVELENGTH, x);
		setOutput(OUT_LEVELW, y);
	}

	@Override
	public Object getSetting() {
		return this.setting;
	}

}
